import sys

def pyKondoModuleImport(moduleName):
    global pyKondoModuleMandatory
    print "Importing modules "
    result={'loaded':[],'mandatory':[],'unloaded':[]}
    for method in [method for method in moduleName if method.startswith('import') ]:
        methodStr = method.replace('import','').lower()
        try:
            pyKondoModuleMandatory=True
            print "  - %s : " % methodStr,
            globals()[method]()
        except ImportError as strerror:
            typeLoading = pyKondoModuleMandatory and 'mandatory' or 'unloaded'
            print "failed %s" % typeLoading
            result[typeLoading].append({'name':methodStr,'loaded':False,'raison':strerror})
        else:
            print "ok" 
            result['loaded'].append({'name':methodStr,'loaded':True})
    if len(result['mandatory']):
        print "################"
        for mod in result['mandatory']:
            print "%s failed due to %s"%(mod['name'],mod['raison'])
        sys.exit()

def importTornado():
    global tornado,define,options
    import tornado.httpserver
    import tornado.ioloop
    import tornado.options
    import tornado.web
    #globals()['define'] = __import__('tornado.options.define')
    #globals()['options'] = __import__('tornado.options.options')
    #from tornado.options import define, options
    
    
def importSimplejson():
    global json
    import simplejson as json
    
def importKondo():
    global pyKondoModuleMandatory
    global pykondo
    pyKondoModuleMandatory=False
    import pykondo 
    
def importOpenCV():
    global pyKondoModuleMandatory
    global opencv
    pyKondoModuleMandatory=False
    import opencv.cv 
    import opencv.highgui
    
global imported
imported = pyKondoModuleImport(dir())
